﻿using Entity;
using IRealize;
using Microsoft.AspNetCore.Mvc;
using Realize;
using static WebApplication9.Controllers.Salary_Disbur_Manag.SDM_insertController;

namespace WebApplication9.Controllers.Salary_Disbur_Manag
{
    public class SDM_selectController : Controller
    {
        private readonly ILogger<SDM_selectController> _logger;
        private readonly IConfiguration configuration;

        public SDM_selectController(ILogger<SDM_selectController> logger, IConfiguration configuration)
        {
            this._logger = logger;
            this.configuration = configuration;
        }
        public IActionResult Index()
        {
            return View();
        }
        public async Task<IActionResult> Index2(int i, int p)
        {
            ISG sg = new SG();
            List<Salary_Grant> zhi = await sg.Server(configuration["SqlServer"]);
            List<Salary_Grant> zhi2 = zhi.Skip((i - 1) * p).Take(p).ToList();
            PagedViewModel<Salary_Grant> cc = new PagedViewModel<Salary_Grant>() { itemas = zhi2, total = zhi.Count };
            return Ok(new
            {
                list = cc
            });
        }
        public async Task<IActionResult> Index3(string sg_id)
        {
            ISGD sgd = new SGD();
            List<Salary_Grant_Details> list = await sgd.Server(sg_id,configuration["SqlServer"]);
            return Ok(new
            {
                zhi = list
            });
        }
        public async Task<IActionResult> Index4(string h_id)
        {
            //ISGD sgd = new SGD();
            IHF hf = new HF();
            List<Human_File> list = await hf.Server3(h_id, configuration["SqlServer"]);
            ISSD ssd = new SSD();
            List<Salary_Standard_Details> list2 = await ssd.Server3(list[0].salary_standard_id, configuration["SqlServer"]);
            return Ok(new
            {
                zhi = list2
            });
        }
        public IActionResult Update(string sg_id,string ss_id) 
        {
            ViewBag.sg_id = sg_id;
            ViewBag.ss_id = ss_id;
            return View();
        }
        public class SalarySubmissionModel
        {
            public List<Salary_Grant_Details> list { get; set; } // 薪酬项目列表
            public string djr { get; set; } // 登记人
            public string djsj { get; set; } // 登记时间
            public string xdh { get; set; } // 单编号
        }
        [HttpPost]
        public async Task<ActionResult> Insert([FromBody] SalarySubmissionModel model)
        {
            ISG sg = new SG();
            IHF hf = new HF();
            ISGD sgd = new SGD();
            double ze = 0;
            foreach (var item in model.list)
            {
                ze += item.salary_paid_sum;
            }
            int i = await sg.Update(model.list[0].salary_grant_id, (decimal)ze,model.djr,DateTime.Parse(model.djsj), configuration["SqlServer"]);
            List<int> i2 = new List<int>();
            if (i > 0) 
            {
                foreach (var item in model.list)
                {
                    i2.Add(await hf.Update2(item.human_id, (decimal)item.salary_paid_sum, configuration["SqlServer"]));
                    i2.Add(await sgd.Update(item.human_id, (decimal)item.bouns_sum, (decimal)item.sale_sum, (decimal)item.deduct_sum, (decimal)item.salary_paid_sum, configuration["SqlServer"]));
                }
                if (i2.Count/2 == model.list.Count) 
                {
                    return Ok(new
                    {
                        zhi = 1
                    });
                }
                return Ok(new
                {
                    zhi = 0
                });

            }

            return Ok(new
            {
                zhi = 0
            });
        }
    }
}
